package com.tjsinfo.tjsoa.useseal.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.jbpm.taskmgmt.exe.TaskInstance;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.useseal.bo.ITUseSealSealBO;
import com.tjsinfo.tjsoa.useseal.vo.TUseSealSeal;
import com.tjsinfo.tjsoa.workflow.WorkFlowTemplate;

public class SealCheckListAction extends DispatchAction {
	private ITUseSealSealBO sealBo;

	public void setSealBo(ITUseSealSealBO sealBo) {
		this.sealBo = sealBo;
	}
	
	
	private List selectList(HttpServletRequest request,String node,Integer sealMark,String sealName) {
		WorkFlowTemplate test = new WorkFlowTemplate();
		List list2 = new ArrayList();
		Integer userId = ToolKit.getSessionUser(request).getUser_id();
		List list = test.selectTask(userId.toString(), node);
		Comparator comp = new Comparator(){
          public int compare(Object o1,Object o2) {
              TaskInstance p1=(TaskInstance)o1;
              TaskInstance p2=(TaskInstance)o2; 
             if(p1.getId()<p2.getId())
                 return 1;
             else
                 return 0;
             }
        };
        Collections.sort(list, comp);
        
        if(sealName == null || "".equals(sealName)){
    		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
    			TaskInstance taskInstance = (TaskInstance) iterator.next();
    			Long flowid =	taskInstance.getProcessInstance().getId();
    			
    			TUseSealSeal tuss = new TUseSealSeal();
    			tuss.setFlowid(Integer.parseInt(flowid.toString()));
    			tuss.setMark(sealMark);
    			if(sealBo.findByExample(tuss).size() > 0 && !"申请人修改".equals(taskInstance.getName()))
    			{
    				tuss = (TUseSealSeal) sealBo.findByExample(tuss).get(0);
    				tuss.setTaskid(taskInstance.getId());
    				list2.add(tuss);
    			}
    		} 	
        }else{
    		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
    			TaskInstance taskInstance = (TaskInstance) iterator.next();
    			Long flowid =	taskInstance.getProcessInstance().getId();
    			
    			TUseSealSeal tuss = new TUseSealSeal();
    			tuss.setFlowid(Integer.parseInt(flowid.toString()));
    			tuss.setMark(sealMark);
    			tuss.setSealName(sealName);
    			if(sealBo.findByExample(tuss).size() > 0 && !"申请人修改".equals(taskInstance.getName()))
    			{
    				tuss = (TUseSealSeal) sealBo.findByExample(tuss).get(0);
    				tuss.setTaskid(taskInstance.getId());
    				list2.add(tuss);
    			}
    		}	
        }
		return list2;
	}	
	
	
	public ActionForward checkList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String node = request.getParameter("node");
		String sealName = request.getParameter("sealName");
		Integer sealMark = Integer.parseInt(request.getParameter("sealMark"));
		
		List<TUseSealSeal> list = selectList(request,node,sealMark,sealName);

		JSONArray array = new JSONArray();
		JSONObject object = new JSONObject();		
		
		for (TUseSealSeal tuss : list) {
			tuss.setDateTime1(sdf.format(tuss.getDateTime()));
			JSONObject tussJson = new JSONObject(tuss);
			array.put(tussJson);
		}
		
		object.put("totalProperty",list.size());
		object.put("root", array);
		
		response.getWriter().print(object);
		
		return null;
	}
	
	public ActionForward checkSelectList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		List<TUseSealSeal> list = null;
		Integer start = 0;
		JSONArray array = new JSONArray();
		JSONObject object = new JSONObject();
		String columnName = request.getParameter("sealName");
		Integer sealMark = Integer.parseInt(request.getParameter("sealMark"));
		
		if (columnName == null || columnName == "") {
			list = sealBo.cheakOverAll(sealMark,request);
		} else {
			list = sealBo.cheakOverColumn(sealMark, "sealName", columnName, request);
		}
		
		for (TUseSealSeal gi : list) {
			JSONObject giJson = new JSONObject(gi);
			array.put(giJson);
		}
		
		object.put("totalProperty",sealBo.cheakOverTotal());
		object.put("root", array);
		response.getWriter().print(object);
		
		return null;
	}
}
